﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.TinhThanhService;
namespace eTMS.Controllers{
public class TinhThanhControllerBase : CommonController{
	public TinhThanhControllerBase()
		: base()
	{	
		this.dao = new TinhThanhServiceClient();
	}
	~TinhThanhControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private TinhThanhEntity oTinhThanh;	
	public TinhThanhEntity TinhThanhObject{
		get{
			return oTinhThanh;
		}
		set{
			oTinhThanh=value;
		}
	}
	protected TinhThanhServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSTinhThanh(false);			
			return _Table;
		}
	}
	public void LoadAllDSTinhThanh(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oTinhThanh==null)return false;
		bool result=true;
		try{
			oTinhThanh = dao.SaveRet(oTinhThanh);
		}
		catch{
			result=false;
		}
		if(result){
			_ListTinhThanh.Add(oTinhThanh);
		}
		return result;
	}
	public bool Update(){
		if(oTinhThanh==null)return false;
		return dao.Update(oTinhThanh);
	}
	public bool Delete(){
		if(oTinhThanh==null)return false;
		bool retvalue = dao.Delete(oTinhThanh);
		if(retvalue){			
			_ListTinhThanh.Remove(oTinhThanh);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 TinhThanh_ID){
		bool retvalue = dao.DeleteByID(TinhThanh_ID);		
		if(retvalue){
			TinhThanhEntity oDeleted=FindTinhThanh(TinhThanh_ID);
			if(oDeleted!=null){
				_ListTinhThanh.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oTinhThanh = new TinhThanhEntity();
	}
    //Copy data from data row in grid to object
    public TinhThanhEntity MapDataRowToObject(DataRow row){
		TinhThanhEntity o = new TinhThanhEntity();
		 o.MaTinhThanh=Convert.ToString(row["MaTinhThanh"]);
		 o.TenTinhThanh=Convert.ToString(row["TenTinhThanh"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("QuocGia_ID"))
			o.QuocGia_ID=(Int32)(row["QuocGia_ID"]);
		 if(!row.IsNull("TinhThanh_ID"))
		    o.TinhThanh_ID=(Int32)(row["TinhThanh_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(TinhThanhEntity o,DataRow row){
		row["MaTinhThanh"]=o.MaTinhThanh;
		row["TenTinhThanh"]=o.TenTinhThanh;
		row["GhiChu"]=o.GhiChu;
		row["QuocGia_ID"]=o.QuocGia_ID;
		row["TinhThanh_ID"]=o.TinhThanh_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.TinhThanhObject,row);
    }
	public void LoadObject(Int32 TinhThanh_ID){
		TinhThanhEntity oLoad=FindTinhThanh(TinhThanh_ID);
		if(oLoad==null){
			oTinhThanh=this.LoadTinhThanh(TinhThanh_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oTinhThanh.IsNullTinhThanh_ID;
			if(!bNotIsNull){
				this.ListTinhThanh.Add(oTinhThanh);
			}
		}
		else{
			oTinhThanh=oLoad;
		}
	}
	#region CachedList TinhThanh
	private static List<TinhThanhEntity> _ListTinhThanh=new List<TinhThanhEntity>();
    public List<TinhThanhEntity>ListTinhThanh{
		get{
			return _ListTinhThanh;
		}
	}
	public static List<TinhThanhEntity>ListTinhThanhStatic{
		get{
			return _ListTinhThanh;
		}
	}
	public TinhThanhEntity FindTinhThanh(Int32 TinhThanh_ID){
		TinhThanhEntity oResult=this.ListTinhThanh.Find(delegate(TinhThanhEntity oFind){
			bool result=true; 
			result&=oFind.TinhThanh_ID.Equals(TinhThanh_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllTinhThanh(bool clearCache){
		if(_ListTinhThanh.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		TinhThanhServiceClient dao = new TinhThanhServiceClient();
		_ListTinhThanh=new List<TinhThanhEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public TinhThanhEntity LoadTinhThanh(Int32 TinhThanh_ID){			
		TinhThanhEntity o=dao.SelectOne(TinhThanh_ID);		
		return o;
	}
	#endregion
}
}
